package com.ibiaoke.boss.controller;

import com.ibiaoke.common.base.BaseController;
import com.ibiaoke.common.base.Page;
import com.ibiaoke.common.response.Res;
import com.ibiaoke.common.utils.AssertUtils;
import com.ibiaoke.customer.entity.Customer;
import com.ibiaoke.goods.dao.XcxShopPriceDao;
import com.ibiaoke.goods.entity.Goods;
import com.ibiaoke.goods.entity.XcxGoods;
import com.ibiaoke.goods.entity.XcxGoodsClass;
import com.ibiaoke.goods.entity.XcxShopPrice;
import com.ibiaoke.goods.service.XcxGoodsClassService;
import com.ibiaoke.goods.service.XcxGoodsService;
import com.ibiaoke.system.entity.Shop;
import com.ibiaoke.system.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class XcxGoodsController extends BaseController {

    @Autowired
    private XcxGoodsService xcxGoodsService;

    @Autowired
    private XcxGoodsClassService xcxGoodsClassService;

    @Autowired
    private ShopService shopService;

    @Autowired
    private XcxShopPriceDao xcxShopPriceDao;

    /**
     * 查询分类类别
     *
     */
    @RequestMapping("/xcxListGoodsClass")
    @ResponseBody
    public Res xcxListGoodsClass() {
        List<XcxGoodsClass> xcxGoodsClass = xcxGoodsClassService.xcxListGoodsClass();
        return Res.create().addResponse("xcxGoodsClass",xcxGoodsClass);
    }


    /**
     * 根据分类查询商品
     *
     * @param cateId
     * @param shopId
     * @param page   @return
     */
    @ResponseBody
    @RequestMapping("/xcxListGoods")
    public Res listGoodsByCateId(Integer cateId, Integer shopId, Page page) {
        List<XcxGoods> xcxGoodsList = xcxGoodsService.xcxListGoodsByGoodsClass(cateId, shopId, page);
        return Res.create().addResponse("xcxGoodsList", xcxGoodsList).addResponse("page", page);
    }

    /**
     * 根据id查询商品详情
     *
     * @param goodsId
     * @param shopId
     * @return
     */
    @ResponseBody
    @RequestMapping("/getXcxGoodsByIdAndShopId")
    public Res getGoods(Integer goodsId, Integer shopId) {
        XcxGoods xcxGoods = xcxGoodsService.getXcxGoodsByGoodsId(goodsId, shopId);
        List<XcxShopPrice> xcxShopPriceList = xcxShopPriceDao.listShopPricesByShopId(goodsId,shopId);
        return Res.create().addResponse("xcxGoods", xcxGoods).addResponse("xcxShopPriceList",xcxShopPriceList);
    }

    /**
     * 通过经纬度来确定商铺然后对商品的库存进行验证
     * @param longitude
     * @param latitude
     * @return
     */
    @ResponseBody
    @RequestMapping("/xcxalidGoods")
    public Res validGoods(@RequestParam(required = false, value = "xcxGoodsIds[]") List<Integer> xcxGoodsIds, Double longitude, Double latitude) {
        //通过选择配送地址，获取最近的店铺
        Shop nearestShop = shopService.getNearestShop(longitude, latitude);
        AssertUtils.notNull(nearestShop, "该地址附近没有店铺");
        XcxGoods xcxGoods = xcxGoodsService.getXcxGoodsByGoodsId(xcxGoodsIds.get(0), nearestShop.getShopId());
        //校验抢购商品
            /*list.forEach(goods -> {
                goodsRushService.validateCount(goods,1,null,false);
            });*/
        return  Res.create().addResponse("xcxGoods",xcxGoods);
    }

}
